package day7;

import java.util.Random;

//this关键字用法
public class User {
    //普通成员变量
    int id;
    String username;
    String password;
    //静态变量
    static double money;
    public User(){
        //注意：避免出现循环引用问题
        //this("",")
        System.out.println("无参构造");
        Random r=new Random();
        this.id=r.nextInt(9000)+1000;

    }
    public User(String username,String password){

        //只能用在构造方法中，而且只能放在第一行
        System.out.println("两个参数构造方法");
        this.username=username;
        this.password=password;
    }
    public User(int id,String username,String password,double money){
        this(username,password);
        System.out.println("四个参数构造方法");
        this.money=money;

    }
    public void setPassword(String password){
        //用法1：this.属性
         this.password=password;
    }
    public void add(User u){
        //假设新增用户的时候需要设置密码
        setPassword(u.password);
        //this.方法()  可用可不用
        this.setPassword(u.password);
    }
    public void print(){
        System.out.println("编号:"+id);
        System.out.println("账号:"+username);
        System.out.println("密码:"+password);
        System.out.println("余额:"+money);
    }

    public static void main(String[] args) {
        User u=new User(0,"admin","123456",1000);
        u.print();
        User u2=new User();
        u2.print();
        User u3=new User();
        System.out.println(u3.money); //不推荐
        System.out.println(User.money); //推荐
    }

}
